<template>
    <div class="category-container">
        <el-card class="article-item" shadow="hover" v-if="infoList.length > 0">
            <el-table v-loading="loading" :data="infoList" style="width: 100%">
                <el-table-column prop="filename" label="文件名">
                </el-table-column>
                <el-table-column prop="fileType" label="文件类型" width="150">
                </el-table-column>
                <el-table-column prop="fileSize" label="文件大小" width="100">
                    <template slot-scope="scope">
                        {{ formatFileSize(scope.row.fileSize) }}
                    </template>
                </el-table-column>
                <el-table-column prop="nickName" label="上传者" width="150">
                </el-table-column>
                <el-table-column prop="downloadNum" label="下载量" width="100">
                </el-table-column>
                <el-table-column prop="likeNum" label="点赞量" width="100">
                </el-table-column>
                <el-table-column label="操作" width="150">
                    <template slot-scope="scope">
                        <el-button size="mini" type="primary" @click="handleLike(scope.row.id)">点赞</el-button>
                        <el-button size="mini" @click="handleDownload(scope.row)">下载</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                    :current-page="pageNum" :page-size="pageSize" :page-sizes="[5, 10, 20, 50]"
                    layout="total, prev, pager, next" :total="total">
                </el-pagination>
            </div>
        </el-card>
        <el-card v-if="infoList.length == 0">
            <el-empty description="暂无数据"></el-empty>
        </el-card>
    </div>
</template>

<script>
import NewPart from '../home/new-part'
import { reqFileShareList, reqFileShareDelete, reqFileShareDownload, reqLikeRecordAdd  } from '@/api'

export default {
    name: 'Resource',
    components: {
        NewPart
    },
    data() {
        return {
            loading: false,
            infoList: [],
            pageNum: 1,
            pageSize: 5,
            total: 0,

        }
    },
    mounted() {
        this.getList();
    },
    methods: {
        handleLike(id) {
            reqLikeRecordAdd({
                targetId: id,
                type: 3
            }).then(response => {
                if (response.data == 1) {
                    this.$message.success('点赞成功');
                } else {
                    this.$message.error('取消点赞');
                }
                this.getList();
            }).catch(error => {
            });
        },
        formatFileSize(size) {
            if (!size) return '0 MB';
            const mb = size / (1024 * 1024);
            return mb.toFixed(2) + ' MB';
        },
        handleDelete(row) {
            this.$confirm('是否确认删除该文件分享?', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(() => {
                return reqFileShareDelete(row.id);
            }).then(() => {
                this.getList();
                this.$message.success("删除成功");
            }).catch(() => { });
        },
        getList() {
            this.loading = true;
            reqFileShareList({
                pageNum: this.pageNum,
                pageSize: this.pageSize
            }).then(res => {
                this.infoList = res.rows;
                this.total = res.total;
                this.loading = false;
            }).catch(error => {
                this.loading = false;
            })
        },
        handleDownload(row) {
            reqFileShareDownload(row.id).then(res => {
                window.open(res.msg, '_blank');
                this.$message.success("下载成功");
                this.getList();
            }).catch(error => {
            })
        },
        handleArticleClick(article) {
            this.$router.push(`/article/detail/${article.id}`);
        },
        handleSizeChange(val) {
            this.pageSize = val;
            this.pageNum = 1;
            this.getList();
        },
        handleCurrentChange(val) {
            this.pageNum = val;
            this.getList();
        }
    },
}
</script>

<style scoped>
.category-container {
    width: 1200px;
    background: #fff;
    border-radius: 8px;
    margin: 0 auto;
    margin-top: 15px;

}

.article-header {
    margin-bottom: 20px;
}

.article-header h2 {
    font-size: 20px;
    color: #303133;
    margin: 0;
    padding-bottom: 10px;
    border-bottom: 2px solid #409EFF;
}

.article-list {
    height: calc(100% - 60px);
    overflow-y: auto;
}

.article-item {
    margin-bottom: 15px;
    cursor: pointer;
    transition: all 0.3s;
}

.article-content {
    padding: 10px;
    display: flex;
    gap: 15px;
}

.article-cover {
    flex-shrink: 0;
    width: 200px;
    height: 120px;
    overflow: hidden;
    border-radius: 4px;
}

.article-cover img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    transition: transform 0.3s;
}

.article-cover img:hover {
    transform: scale(1.05);
}

.article-info {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
}

.article-title {
    font-size: 16px;
    color: #303133;
    margin: 0 0 10px 0;
    font-weight: 600;
}

.article-summary {
    font-size: 14px;
    color: #606266;
    margin: 0 0 10px 0;
    line-height: 1.5;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    overflow: hidden;
    flex: 1;
}

.article-footer {
    display: flex;
    align-items: center;
    font-size: 12px;
    color: #909399;
}

.article-time,
.article-views {
    margin-right: 15px;
    display: flex;
    align-items: center;
}

.article-time i,
.article-views i {
    margin-right: 4px;
}

/* 自定义滚动条样式 */
.article-list::-webkit-scrollbar {
    width: 6px;
}

.article-list::-webkit-scrollbar-thumb {
    background: #dcdfe6;
    border-radius: 3px;
}

.article-list::-webkit-scrollbar-track {
    background: #f5f7fa;
}

.pagination-container {
    display: flex;
    justify-content: center;
    margin-top: 10px;
}
</style>